﻿// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

Shader "GJ/Item" {
Properties {
    [PerRendererData] _MainTex ("Main Texture", 2D) = "black" {}
    _Color ("Main Color", Color) = (1,1,1,1)
    _SColor ("Second Color", Color) = (1,1,1,1)
    _BackgroundTex ("Background Texture", 2D) = "black" {}
    _OrderTex ("Order Texture", 2D) = "black" {}
    _OrderColor ("Order Color", Color) = (1,1,1,1)
    _ColSpeed("ColSpeed", Range(0, 10)) = 0
    _ColWeight("ColWeight", Range(-1, 1)) = 0
    _ScaleSize("ScaleSize", Range(-1, 1)) = 0
    // mask
    _StencilComp("Stencil Comparison", Float) = 8
    _Stencil("Stencil ID", Float) = 0
    _StencilOp("Stencil Operation", Float) = 0
    _StencilWriteMask("Stencil Write Mask", Float) = 255
    _StencilReadMask("Stencil Read Mask", Float) = 255
    _ColorMask("Color Mask", Float) = 15
}
SubShader {
    Stencil
    {
        Ref[_Stencil]
        Comp[_StencilComp]
        Pass[_StencilOp]
        ReadMask[_StencilReadMask]
        WriteMask[_StencilWriteMask]
    }
    ColorMask[_ColorMask]

    Tags {
        "IgnoreProjector"="True"
        "Queue"="Transparent"
        "RenderType"="Transparent"
        "PreviewType"="Plane"
		"CanUseSpriteAtlas"="True"
    }
    Pass {
        Blend SrcAlpha OneMinusSrcAlpha
        Cull Off
        Lighting Off
        ZWrite Off
//        Ztest Always
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0

        #include "UnityCG.cginc"

        sampler2D _MainTex; float4 _MainTex_ST;
        sampler2D _BackgroundTex;
        sampler2D _OrderTex;
        fixed4 _Color;
        fixed4 _SColor;
        fixed4 _OrderColor;
        fixed _ColSpeed;
        fixed _ColWeight;
        fixed _ScaleSize;

        struct appdata_t {
            float4 vertex : POSITION;
            fixed4 color : COLOR;
            float2 texcoord : TEXCOORD0;
            UNITY_VERTEX_INPUT_INSTANCE_ID
        };

        struct v2f {
            float4 vertex : SV_POSITION;
            fixed4 color : COLOR;
            float2 texcoord : TEXCOORD0;
            UNITY_VERTEX_OUTPUT_STEREO
        };

        

        half4 blend_color(half4 c1, half4 c2)
        {
            half4 result = half4(0, 0, 0, max(c1.a, c2.a));
            result.rgb = c1.rgb * (1 - c2.a) + c2.rgb * c2.a;
            return result;
        }
        
        v2f vert (appdata_t v)
        {
            v2f o;
            UNITY_SETUP_INSTANCE_ID(v);
            UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.color = v.color;
            o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            _ScaleSize *= -1;
            float2 uv = i.texcoord * (_ScaleSize + 1) + (_ScaleSize * 0.5 * -1);  
            fixed4 tex = tex2D(_MainTex, uv);
            fixed4 col = tex2D(_BackgroundTex, i.texcoord) * i.color;
            if (_ColSpeed != 0)
            {
                float t = abs(sin(_ColSpeed * _Time.y)) + _ColWeight;
                col = lerp(_Color * col, _SColor * col, t);
            }
            if (tex.a < 1)
            {
                col = blend_color(col, tex);
            }
            else
            {
                col = tex;
            }
            return blend_color(col, tex2D(_OrderTex, i.texcoord) * _OrderColor);
        }
        ENDCG
    }
}

}